<?xml version="1.0" encoding="UTF-8"?>
<project name="UnifiedCloudAPI" default="build" basedir=".">

	<!--***************************-->
	<!-- VARIABLES AND DIRECTORIES -->
	<!--***************************-->
	
	<!-- Version and build number -->
	<property name="version.num" value="1.00" />
	<buildnumber file="build.num" />
	
	<!-- Directories -->
	<property name="build.dir" value="./build" />
	<property name="docs.dir" value="./docs" />
	<property name="lib.dir" value="./lib" />
	<property name="release.dir" value="./release" />
	<property name="src.dir" value="./src" />
	<property name="test.dir" value="./test" />
	<property name="javadoc.dir" value="${docs.dir}/javadoc" />
	<property name="test-reports.dir" value="${docs.dir}/test-reports" />
	
	<!--************-->
	<!-- CLASSPATHS -->
	<!--************-->
	
	<!-- Manifest's classpath -->
	<property name="manifest.classpath" value=""/>
	
	<!-- Main classpath -->
	<path id="lib.classpath">
		<!-- Dependencies -->
		<pathelement path="${lib.dir}/xstream-1.4.2.jar"/>
		
		<!-- Provider APIs -->
		<pathelement path="${lib.dir}/amazon/aws-java-sdk-1.3.15.jar"/>
		<pathelement path="${lib.dir}/azure/microsoft-windowsazure-api-0.3.1-custom.jar"/>
		<pathelement path="${lib.dir}/rackspace/java-cloudfiles.jar"/>
		
		<!-- Provider APIs' dependencies -->
		<pathelement path="${lib.dir}/dependencies/commons-codec-1.3/commons-codec-1.3.jar"/>
		<pathelement path="${lib.dir}/dependencies/commons-lang-2.4/commons-lang-2.4.jar"/>
		<pathelement path="${lib.dir}/dependencies/commons-logging-1.1.1/commons-logging-1.1.1.jar"/>
		<pathelement path="${lib.dir}/dependencies/httpcomponents-client-4.1.1/httpclient-4.1.1.jar"/>
		<pathelement path="${lib.dir}/dependencies/httpcomponents-client-4.1.1/httpcore-4.1.jar"/>
		<pathelement path="${lib.dir}/dependencies/log4j-1.2.15/log4j-1.2.15.jar"/>
	</path>
	
	<!-- Tests' classpath -->
	<path id="test.classpath">
		<!-- Dependencies -->
		<pathelement path="${lib.dir}/junit-4.10.jar"/>
		
		<!-- Libraries -->
		<path refid="lib.classpath"/>
		
		<!-- Compiled classes -->
		<pathelement path="${build.dir}"/>
	</path>
	
	<!--*******-->
	<!-- TASKS -->
	<!--*******-->
	
	<target name="init" description="Initializes all variables necessary for the compile and build tasks">
		<!-- Initialize variables -->
		<tstamp>
			<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
		</tstamp>
		
		<echo message="Initial variables successfully initialized." />
	</target>
	
	<target name="compile" description="Compiles all source files contained in the src directory" depends="init">
		<!-- Create necessary directories -->
		<mkdir dir="${build.dir}" />
		
		<!-- Delete previously compiled classes -->
		<delete includeemptydirs="true">
			<fileset dir="${build.dir}" includes="**/*"/>
		</delete>
		
		<!-- Compile classes -->
		<javac srcdir="${src.dir}" destdir="${build.dir}" includeAntRuntime="false" includeJavaRuntime="false">
			<classpath refid="lib.classpath"/>
		</javac>
		
		<echo message="Source code successfully compiled." />
	</target>
	
	<target name="build" description="Packages all JAR files" depends="compile,init">
		<!-- Create necessary directories -->
		<mkdir dir="${build.dir}" />
		<mkdir dir="${javadoc.dir}" />
		
		<!-- Delete previously created javadoc files -->
		<delete includeemptydirs="true">
			<fileset dir="${javadoc.dir}" includes="**/*"/>
		</delete>
		
		<!-- Create manifest -->
		<manifest file="./MANIFEST.MF">
			<attribute name="Implementation-Version" value="${version.num}-${build.number}" />
			<attribute name="Built-By" value="Pablo Danilo Aliscioni"/>
			<attribute name="Built-Date" value="${TODAY}" />
			<attribute name="Class-Path" value="${manifest.classpath}"/>
   		</manifest>
   		
		<!-- Create version file -->
		<delete file="${build.dir}/version.txt" failonerror="false" />
		<echo file="${build.dir}/version.txt">Unified Cloud API${line.separator}${line.separator}Version: ${version.num}${line.separator}Build: ${build.number}${line.separator}Date: ${TODAY}</echo> 
   		
		<!-- Copy license file -->
		<delete file="${build.dir}/license.txt" failonerror="false" />
		<copy file="./license.txt" tofile="${build.dir}/license.txt" />
		
		<!-- Create and copy JAR file -->
		<jar jarfile="${release.dir}/v${version.num}/UnifiedCloudAPI-${version.num}-${build.number}.jar" manifest="./MANIFEST.MF" >
			<fileset dir="${build.dir}" excludes="**/*Test.class"/>
		</jar>
		<copy file="${release.dir}/v${version.num}/UnifiedCloudAPI-${version.num}-${build.number}.jar" tofile="${release.dir}/UnifiedCloudAPI-latest.jar" />
		
		<echo message="JAR file successfully packaged." />
		
		<!-- Create and zip javadoc files -->
        <javadoc packagenames="ar.com.pabloaliscioni.cloud.api.*" windowtitle="Unified Cloud API" sourcepath="${src.dir}" destdir="${javadoc.dir}" doctitle="Unified Cloud API" verbose="no"
        	access="public" author="true" splitindex="true" use="true" version="true" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" source="1.7"
			classpath="lib/xstream-1.4.2.jar;lib/amazon/aws-java-sdk-1.3.15.jar;lib/azure/microsoft-windowsazure-api-0.3.0.jar;lib/rackspace/java-cloudfiles.jar;lib/dependencies/commons-codec-1.3/commons-codec-1.3.jar;lib/dependencies/commons-lang-2.4/commons-lang-2.4.jar;lib/dependencies/commons-logging-1.1.1/commons-logging-1.1.1.jar;lib/dependencies/httpcomponents-client-4.1.1/httpclient-4.1.1.jar;lib/dependencies/httpcomponents-client-4.1.1/httpcore-4.1.jar;lib/dependencies/log4j-1.2.15/log4j-1.2.15.jar">
        </javadoc>
		<zip destfile="${release.dir}/v${version.num}/UnifiedCloudAPI-${version.num}-${build.number}-javadoc.zip" basedir="${javadoc.dir}" />
		
		<echo message="Javadoc successfully generated." />
	</target>
	
	<target name="test" description="Executes all JUnit tests contained in the test directory" depends="compile,init">
		<!-- Compile test files -->
		<javac srcdir="${test.dir}" destdir="${build.dir}" includeAntRuntime="false" includeJavaRuntime="false">
			<classpath refid="test.classpath"/>
		</javac>
		
		<echo message="JUnit tests successfully compiled." />
		
		<!-- Run JUnit tests -->
		<junit showoutput="yes" haltonfailure="no" printsummary="yes" >
			<classpath>
				<pathelement path="${build.dir}"/>
				<pathelement path="${test.dir}"/>
				<fileset dir="${lib.dir}">
					<include name="**/*.jar"/>
				</fileset>
			</classpath>
			<batchtest todir="${test-reports.dir}" fork="yes">
				<formatter type="plain"/>
					<fileset dir="${test.dir}">
						<include name="**/*Tests.java"/>
					</fileset>
			</batchtest>
		</junit>
		
		<echo message="JUnit tests successfully executed." />
	</target>
	
	<target name="all" description="Execute all tasks" depends="build,test" />
</project>